;$(function () {
    var serverIdArr=[];
    upload(serverIdArr);
    // getInfo(id,serverIdArr);
    contribute_validator();
    getList();
});

var tale = new $.tale();//弹框
function upload(serverIdArr){
    var myDropzone = new Dropzone("#dropzone", {
        paramName: "file",
        url: ctxPath+"/file/upload",
        addRemoveLinks: true,
        dictRemoveLinks: "x",
//        	        uploadMultiple: true,
        maxFiles: 3,
        maxFilesize: 10,
        init: function () {
            this.on("success", function (file, msg) {
                file.serverId=msg.message;
                serverIdArr.push(msg.message);
                $("#image_files").val(serverIdArr);
            });
            this.on('error', function (files, response) {
            });
            //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
            this.on("removedfile", function (file) {
                serverIdArr.splice($.inArray(file.serverId,serverIdArr),1);
                $("#image_files").val(serverIdArr);
            });
        }
    });
}

function contribute_validator() {
    $("#global-form").validator({
        fields: {
            name: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            sort: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            files: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
        },
        invalid: function (form, errors) {
            return;
        },
        //验证成功
        valid: function (form) {
            saveInfo();
        },
    });
}

function saveInfo() {
    var classId = $("#classId").val();
    var json =$("#global-form").serializeFormJSON();
    $.ajax({
        url: ctxPath+"/party/class/saveClassDetailInfo",
        type: 'POST',
        data: json,
        success: function (d) {
            var success = d.success;
            if (success) {
                tale.alertOk({
                    text: '课程保存成功',
                    then: function () {
                        setTimeout(function () {
                            window.location.href = ctxPath+'/party/class/detailNews/'+classId;
                        }, 500);
                    }
                });
            } else {
                tale.alertError(result.message || '保存失败');
            }
        }
    });
}


function getList(pageNo, pageSize) {
    if (typeof(pageNo) == 'undefined') {
        pageNo = 1;
    }

    if (typeof(pageSize) == 'undefined') {
        pageSize = 15;
    }

    var classId = $("#classId").val();

    $.ajax({
        url: ctxPath+"/party/class/getDetailPage",
        type: 'POST',
        data: {pageNo: pageNo, pageSize: pageSize,classId:classId},
        success: function (d) {
            if (d.data != null) {
                var html = "";
                $.each(d.data.dataList, function (i, item) {
                    html += toHtml(item);
                })
                $("#list_result").html(html);
            }
            getPage(d.data.pages, d.data.total);
        }
    });
}

//分页
function getPage(pageCount, totalCount) {
    kkpager.total = pageCount;//总页码
    kkpager.totalRecords = totalCount;//总条数
    kkpager.generPageHtml({
        pno: 0,
        mode: 'click',//默认值是link，可选link或者click
        click: function (n) {
            cur.constant.pageNo = n;
            getList(n, 15);
            this.selectPage(n);
            return false;
        }
    });
}

function toHtml(data) {
    var img = '/image/file.png';
    var del = "<button type='button' class='btn btn-danger btn-sm' disabled='disabled'><i class=\"fa fa-trash-o\"></i>删除</button>";
    if (data.fileEOList != null && data.fileEOList.length > 0) {
    	var remark2 = data.fileEOList[0].remark2;
    	if(remark2!=1){
    		img = ctxPath+'/file/showImage/' + data.files[0];
    	}
        del = "<button type='button' class='btn btn-danger btn-sm' onclick='del(" + data.id + ")'><i class=\"fa fa-trash-o\"></i>删除</button>";
    }

    var createTime = new Date(data.createTime).Format("yyyy-MM-dd");

    return '<tr>'
        + '<td>'
        + '<img src="' + img + '" width="30px;">&nbsp;&nbsp;&nbsp;&nbsp;' + data.name
        + '</td>'
        + '<td class="text-center">'
        + data.sort
        + '</td>'
        + '<td class="text-center">'
        + createTime
        + '</td>'
        + '<td class="text-center">'
        + del
        + '</td>'
        + '</tr>';
}

function del(id) {
	 var classId = $("#classId").val();
    $.ajax({
        url: ctxPath+"/party/class/delDetail/"+id,
        type: 'GET',
        success: function (d) {
            console.log(d);
            if(d.success){
//                setTimeout(function () {
//                    window.location.href = '/party/class/detailNews/'+classId;
//                }, 500);
                getList();
            }

           
        },
        error: function () {
            alert("异常！");
        }
    });
}
